package com.gameproject.alltogether;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;




public class SpriteObject {
	
	protected long id;
	protected Bitmap bitmap;
	protected double x;		
	protected double y;	
	protected double x_move = randomMoveRate();
	protected double y_move = randomMoveRate();

	protected int state;
	

	public SpriteObject(Bitmap bitmap, int x, int y, long id) {
		this.id = id;
		this.bitmap = bitmap;
		this.x = x;
		this.y = y;
	}
	
	protected double randomMoveRate() {
		double r = Math.random();
		if (r > 0.5)
			return Constants.MOVE_RATE;
		return -Constants.MOVE_RATE;
	}

	public int getstate(){
		return state;
	}
	public void setstate(int s){
		state = s;
	}

	
	public double getX() {
		return x;
	}
	
	public double getY() {
		return y;
	}
	
	public Bitmap getBitmap() {
		return bitmap;
	}
	
	
	public void setMoveX(double speedx){
		x_move = speedx;
	}
	public void setMoveY(double speedy){
		y_move = speedy;
	}
	public void setX(int x) {
		this.x = x;
	}

	public void setY(int y) {
		this.y = y;
	}
	public void setBitmap(Bitmap bitmap) {
		this.bitmap = bitmap;
	}

	public void draw(Canvas canvas) {
		canvas.drawBitmap(bitmap, (int)x - (bitmap.getWidth() / 2), (int)y - (bitmap.getHeight() / 2), null);
	}

	public void update(int adj_mov) {
			x += (adj_mov * x_move);
			y += (adj_mov * y_move);
	}
	
	public boolean collide(SpriteObject entity){		
		Rect me = this.getRectangle();
		Rect other = entity.getRectangle();
		if (Rect.intersects(me, other))
			return true;
		return false;
	}

	public double getMoveX() {
		return x_move;
	}

	public double getMoveY() {
		return y_move;
	}
	
	public Rect getRectangle(){
		int left = (int) (x - (bitmap.getWidth()/2) - Constants.COLLISION_DELTA);
		int right = (int) (x + (bitmap.getWidth()/2) + Constants.COLLISION_DELTA);
		int top = (int) (y - (bitmap.getHeight()/2) - Constants.COLLISION_DELTA);
		int bottom = (int) (y + (bitmap.getHeight()/2) + Constants.COLLISION_DELTA);
		
		return new Rect(left, top, right, bottom);
		
	}
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

}
